﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightCoverage.RepRef;
using System.Windows.Browser;
using Visifire.Charts;
using Visifire.Commons;

namespace SilverlightCoverage
{
    public partial class CovChart : UserControl
    {
        System.Collections.ObjectModel.ObservableCollection<ClassInfo> classInfoCollection;
        IsimpleWcfClient proxy = new IsimpleWcfClient();
        string moduleName = string.Empty;
        string namespaceName = string.Empty;
        string key = string.Empty;
        string value = string.Empty;
        string rptFile = string.Empty;

        public CovChart()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CovChart_Loaded);
        }

        void CovChart_Loaded(object sender, RoutedEventArgs e)
        {
            rptFile = (Application.Current as SilverlightCoverage.App).strxmlpath;
            proxy.GetClassInfoAppLevelSLCompleted += new EventHandler<GetClassInfoAppLevelSLCompletedEventArgs>(proxy_GetClassInfoAppLevelSLCompleted);
            proxy.GetClassInfoAppLevelSLAsync(moduleName, namespaceName, rptFile);
        }

    

        void proxy_GetClassInfoAppLevelSLCompleted(object sender, GetClassInfoAppLevelSLCompletedEventArgs e)
        {
            classInfoCollection = e.Result;

            IDictionary<string, int> mycoll = new Dictionary<string, int>();
            foreach (ClassInfo cinfo in classInfoCollection)
            {
                string valu=string.Empty;
                if (cinfo.CovPercentage >= 100)
                {
                     valu = "100%";
                }

                else if (cinfo.CovPercentage >= 90)
                {
                    valu = "90-99%";
                }
                else if (cinfo.CovPercentage >= 80)
                {
                    valu = "80-89%";
                }
                else if (cinfo.CovPercentage >= 70)
                {
                    valu = "70-79%";
                }
                else if (cinfo.CovPercentage >= 60)
                {
                    valu = "60-69%";
                }
                else if (cinfo.CovPercentage >= 50)
                {
                    valu = "50-59%";
                }
                else if (cinfo.CovPercentage >= 40)
                {
                    valu = "40-49%";
                }
                else if (cinfo.CovPercentage >= 30)
                {
                    valu = "30-39%";
                }
                else if (cinfo.CovPercentage >= 20)
                {
                    valu = "20-29%";
                }
                else if (cinfo.CovPercentage >= 10)
                {
                    valu = "10-19%";
                }
                else if (cinfo.CovPercentage >= 0)
                {
                    valu = "0-9%";
                }

                Int32 index;

                if (mycoll.TryGetValue(valu, out index))
                {
                    mycoll[valu] = index + 1;
                }
                else
                {
                    mycoll.Add(valu, 1); 
                }
 
            }

            Chart ch = new Chart();

            
            DataSeries ds = new DataSeries();
            ds.RenderAs = RenderAs.Column;
            ds.LabelLineStyle = LineStyles.Solid;
            ds.SelectionEnabled = true;
            
            DataPoint dp;

            //var items = from k in mycoll.Keys
            //            orderby mycoll[k] ascending
            //            select k;


            //foreach (KeyValuePair<string,int> cinfo in mycoll)
            //{
            //    dp = new DataPoint();
            //    dp.AxisXLabel = cinfo.Key;
            //    dp.YValue = cinfo.Value;
            //    ds.DataPoints.Add(dp);
            //}

            //foreach (string k in items)
            //{
            //    dp = new DataPoint();
            //    dp.AxisXLabel = k;
            //    dp.YValue = mycoll[k];
            //    ds.DataPoints.Add(dp);
            //}

            if (mycoll.ContainsKey("0-9%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "0-9%";
                dp.YValue = mycoll["0-9%"];
                dp.Color = new SolidColorBrush(Colors.Red);
                dp.ToolTipText = mycoll["0-9%"] + " Classes got 0-9% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "0-9%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Red);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("10-19%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "10-19%";
                dp.YValue = mycoll["10-19%"];
                dp.Color = new SolidColorBrush(Colors.Red);
                dp.ToolTipText = mycoll["10-19%"] + " Classes got 10-19% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "10-19%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Red);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("20-29%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "20-29%";
                dp.YValue = mycoll["20-29%"];
                dp.Color = new SolidColorBrush(Colors.Red);
                dp.ToolTipText = mycoll["20-29%"] + " Classes got 20-29% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "20-29%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Red);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("30-39%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "30-39%";
                dp.YValue = mycoll["30-39%"];
                dp.Color = new SolidColorBrush(Colors.Red);
                dp.ToolTipText = mycoll["30-39%"] + " Classes got 30-39% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "30-39%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Red);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("40-49%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "40-49%";
                dp.YValue = mycoll["40-49%"];
                dp.Color = new SolidColorBrush(Colors.Blue);
                dp.ToolTipText = mycoll["40-49%"] + " Classes got 40-49% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "40-49%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Blue);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("50-59%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "50-59%";
                dp.YValue = mycoll["50-59%"];
                dp.Color = new SolidColorBrush(Colors.Blue);
                dp.ToolTipText = mycoll["50-59%"] + " Classes got 50-59% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "50-59%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Blue);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("60-69%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "60-69%";
                dp.YValue = mycoll["60-69%"];
                dp.Color = new SolidColorBrush(Colors.Blue);
                dp.ToolTipText = mycoll["60-69%"] + " Classes got 60-69% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "60-69%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Blue);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("70-79%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "70-79%";
                dp.YValue = mycoll["70-79%"];
                dp.Color = new SolidColorBrush(Colors.Green);
                dp.ToolTipText = mycoll["70-79%"] + " Classes got 70-79% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "70-79%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Green);
                ds.DataPoints.Add(dp);
            }
            if (mycoll.ContainsKey("80-89%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "80-89%";
                dp.YValue = mycoll["80-89%"];
                dp.Color = new SolidColorBrush(Colors.Green);
                dp.ToolTipText = mycoll["80-89%"] + " Classes got 80-89% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "80-89%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Green);

                ds.DataPoints.Add(dp);
            }

            if (mycoll.ContainsKey("90-99%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "90-99%";
                dp.YValue = mycoll["90-99%"];
                dp.Color = new SolidColorBrush(Colors.Green);
                dp.ToolTipText = mycoll["90-99%"] + " Classes got 90-99% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "90-99%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Green);
                ds.DataPoints.Add(dp);
            }

            if (mycoll.ContainsKey("100%"))
            {
                dp = new DataPoint();
                dp.AxisXLabel = "100%";
                dp.YValue = mycoll["100%"];
                dp.Color = new SolidColorBrush(Colors.Green);
                dp.ToolTipText = mycoll["100%"] + " Classes got 100% Coverage";
                ds.DataPoints.Add(dp);
            }

            else
            {
                dp = new DataPoint();
                dp.AxisXLabel = "100%";
                dp.YValue = 0;
                dp.Color = new SolidColorBrush(Colors.Green);
                ds.DataPoints.Add(dp);

            }
            Axis ax = new Axis();
            ax.Title = "Coverage Percentage";
            ch.AxesX.Add(ax);

            Axis ax1 = new Axis();
            ax1.Title = "Class Count";
            ch.AxesY.Add(ax1);

            ch.Series.Add(ds);
            LayoutRoot.Children.Add(ch);
        }
    }
}
